package com.sgb.kjwl.viewmodel.quotedprice;


import android.app.Application;
import android.text.TextUtils;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.TeamPriceItemEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QuotedPriceModel extends AndroidViewModel {


    private PickSelectEntity all;

    private Map map;
    private int page = 1;
    private int limit = 10;
    public MutableLiveData<Boolean> loading = new MutableLiveData<>();
    public MutableLiveData<List<TeamPriceItemEntity>> priceList = new MutableLiveData<>();
    public MutableLiveData<String> h5Url = new MutableLiveData<>();
    public MutableLiveData<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>> categoryType = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> workType = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> city = new MutableLiveData<>();

    public QuotedPriceModel(Application application) {
        super(application);
        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");
    }

    public void getList(String firstCategoryNo, String secondCategoryNo, String thirdCategoryNo, String typeWork, String provinceNo, String cityNo, String areaNo, boolean b) {
        map = new HashMap();
        page = b ? page + 1 : 1;
        map.put("limit", limit);
        map.put("page", page);
        if (!"".equals(firstCategoryNo) && firstCategoryNo != null) {
            map.put("firstCategoryNo", firstCategoryNo);
        }
        if (!"".equals(secondCategoryNo) && secondCategoryNo != null) {
            map.put("secondCategoryNo", secondCategoryNo);
        }
        if (!"".equals(thirdCategoryNo) && thirdCategoryNo != null) {
            map.put("thirdCategoryNo", thirdCategoryNo);
        }
        if (!TextUtils.isEmpty(typeWork)) {
            map.put("typeWork", typeWork);
        }
        if (!TextUtils.isEmpty(provinceNo)) {
            map.put("provinceNo", provinceNo);
        }
        if (!TextUtils.isEmpty(cityNo)) {
            map.put("cityNo", cityNo);
        }
        if (!TextUtils.isEmpty(areaNo)) {
            List<String> stringList = new ArrayList<>();
            stringList.add(areaNo);
            map.put("areaNoList", stringList);
        }
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().findPriceList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<TeamPriceItemEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<TeamPriceItemEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                priceList.postValue(data.getData().getList());
            }
        });
    }


    public void getUrl(String id) {
        loading.postValue(true);
        APIManager.getInstance().getTeamPriceAPI().getUrlSetting("offerdetail").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                String url = data.getData().getUrl() + "?id=" + id;
                h5Url.postValue(url);
            }
        });
    }

    public void getCategoryType() {
        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getCategoryListTree().enqueue(new ICallBack<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                data.getData().add(0,new PickFilterEntity("","全部",""));
                categoryType.postValue(data.getData());
            }
        });
    }

    public void getWorkType() {
        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getWorkType(2).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
                    @Override
                    public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                        DialogHelper.dismissProgressDialog();
                        List<PickSelectEntity> entityList = data.getData();
                        entityList.add(0, all);
                        workType.postValue(entityList);
                    }
                });
    }


    public void getCityList() {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getCityList2().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                city.postValue(data.getData());
            }
        });
    }

}
